package edu.pku.se.chocolate.UI.Adapter;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.UI.View.FavoritesView;
import edu.pku.se.chocolate.datastructure.Message;

public class FavoritesListAdapter extends BaseExpandableListAdapter{
	
	private Context mContext;
	private ArrayList<Message> ms;

	private final int childSize = 1;
	private final Button bt_del;
	private int times_click = 0;
	public FavoritesListAdapter(Context mc, View _v, final FavoritesView _fv) {
		mContext = mc;

		ms = ChocolateActivity.c_solver.getFavouriteMessage(0);

		bt_del = (Button)_v.findViewById(R.id.fdel);
		TextView ftv = (TextView)_v.findViewById(R.id.ftv);
		ftv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
		ftv.setTextColor(Color.BLACK);
		ftv.setGravity(Gravity.CENTER);
		
		setDeleteAble(false);
		bt_del.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Builder sureDel = new AlertDialog.Builder(mContext);
				sureDel.setTitle("确认删除待删的收藏条目");
				sureDel.setMessage("确定要删除吗？");
				sureDel.setNegativeButton("取消", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						
					}					
				});
				sureDel.setPositiveButton("删除", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						for(int i = 0; i < ms.size(); i ++)
						{
							if(ms.get(i).favorite == 0)
							{
								ChocolateActivity.c_solver.cancleCollectMessage(ms.get(i));	
							}
						}
						_fv.show();
					}

					
				});
				sureDel.create();
				sureDel.show();
			}
			
		});
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//groupPosition--;
		LinearLayout ll;
//		if(groupPosition == -1 || groupPosition == (ms.size()))
//			return new LinearLayout(mContext);
		TextView tv;
		TextView tvmore;
//		if(convertView == null) {
			ll = new LinearLayout(mContext);
			tv = new TextView(mContext);
			tvmore = new TextView(mContext);
			AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
			tv.setLayoutParams(layoutParams);
			ll.setOrientation(LinearLayout.VERTICAL);
			ll.addView(tv);
			ll.addView(tvmore);
//		}
//		else {
//			ll = (LinearLayout) convertView;
//			tv = (TextView) ll.getChildAt(0);
//		}
			tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
			tvmore.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
			tv.setTextColor(0xff98f5ff);
//		switch(childPosition) {
//		case 0:
			String temp = ms.get(groupPosition).summary;
			if(temp.equals("removeBBS Error!"))
				temp = "详情猛击更多！！";
			tv.setText(ToDBC(temp)+"\n"+ms.get(groupPosition).time);
//			break;
//		case 1:
//			tv.setText(ms.get(groupPosition).time);
//			break;
			tvmore.setText("更多...");
			tvmore.setTextColor(android.graphics.Color.GREEN);
			final int position = groupPosition;
			tv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub  
					 Uri content_url = Uri.parse(ms.get(position).link);   
					 Intent intent = new Intent(Intent.ACTION_VIEW, content_url);  
					 mContext.startActivity(intent); 
				}
				
			});
			
		return ll;
	}
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childSize;
	}
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return ms.get(groupPosition);
	}
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return ms.size();
//		return ms.size()+2;
	}
	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
//		groupPosition--;
		LinearLayout ll;
		ll = new LinearLayout(mContext);
		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//		if(groupPosition == -1){
//			final Button update = new Button(mContext);
//			layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
//			update.setLayoutParams(layoutParams);
//			update.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
//			update.setText("更新");
//			update.setTextColor(android.graphics.Color.WHITE);
//			update.setBackgroundColor(android.graphics.Color.BLACK);
//			update.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
//			update.setOnTouchListener(new OnTouchListener(){
//
//				@Override
//				public boolean onTouch(View arg0, MotionEvent event) {
//					// TODO Auto-generated method stub
//						if(event.getAction() == MotionEvent.ACTION_UP){
//							update.setBackgroundColor(android.graphics.Color.BLACK);
//						} 
//						if(event.getAction() == MotionEvent.ACTION_DOWN){
//							update.setBackgroundColor(android.graphics.Color.YELLOW);
//						}
//					return false;
//				}
//				
//			});
//			ll.addView(update);
//			return ll;
//		}
//		else if(groupPosition == (ms.size())) {
//			final Button update = new Button(mContext);
//			layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
//			update.setLayoutParams(layoutParams);
//			update.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
//			update.setText("更多");
//			update.setTextColor(android.graphics.Color.WHITE);
//			update.setBackgroundColor(android.graphics.Color.BLACK);
//			update.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
//			update.setOnTouchListener(new OnTouchListener(){
//
//				@Override
//				public boolean onTouch(View arg0, MotionEvent event) {
//					// TODO Auto-generated method stub
//						if(event.getAction() == MotionEvent.ACTION_UP){
//							update.setBackgroundColor(android.graphics.Color.BLACK);
//						} 
//						if(event.getAction() == MotionEvent.ACTION_DOWN){
//							update.setBackgroundColor(android.graphics.Color.YELLOW);
//						}
//					return false;
//				}
//				
//			});
//			ll.addView(update);
//			return ll;
//		}
		TextView tv;
		tv = new TextView(mContext);
		tv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
		tv.setLayoutParams(layoutParams);
		// Center the text vertically
		tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// Set the text starting position
		tv.setPadding(5, 0, (int) (ChocolateActivity.DM.widthPixels*45d/480d), 0);
		tv.setText(ToDBC(ms.get(groupPosition).title));
		final TextView tv2 = new TextView(mContext);
		tv2.setText("收藏");
		layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		tv2.setLayoutParams(layoutParams);
		tv2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		tv2.setPadding(0, 0, 0, 0);
		tv2.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
		ll.addView(tv2);
		ll.addView(tv);
		if(ms.get(groupPosition).favorite!=0)
			tv2.setTextColor(android.graphics.Color.WHITE);
		else tv2.setTextColor(android.graphics.Color.GRAY);
		final int position = groupPosition;
		tv2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(ms.get(position).favorite!=0) {
					tv2.setTextColor(android.graphics.Color.GRAY);
					tv2.setText("待删");
					ms.get(position).favorite = 0;
					times_click ++;
					if(times_click == 1)
						bt_del.setEnabled(true);//.setClickable(true);
				}
				else {
					tv2.setTextColor(android.graphics.Color.WHITE);
					tv2.setText("收藏");
					ms.get(position).favorite = 1;
					times_click --;
					if(times_click == 0)
					{
						bt_del.setEnabled(false);//.setClickable(false);
					}					
				}
			}
			
		});
//		if(ms.get(groupPosition).isRead == 1) {
//			tv.setTextColor(android.graphics.Color.GRAY);
//		}
//		else {
			tv.setTextColor(android.graphics.Color.WHITE);
//		}
		return ll;
	}
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
//		if(groupPosition==0||groupPosition==(ms.size()+1))
//			return false;
		return true;
	}
	
	public void setDeleteAble(boolean _b)
	{
		bt_del.setEnabled(_b);//.setClickable(_b);
	}
	
	private String ToDBC(String input) {    
        char[] c = input.toCharArray();    
        for (int i = 0; i < c.length; i++) {    
            if (c[i] == 12288) {    
                c[i] = (char) 32;    
                continue;    
            }    
            if (c[i] > 65280 && c[i] < 65375)    
                c[i] = (char) (c[i] - 65248);    
        }    
        return new String(c);    
    }
	
}
